﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Group16.Class_Library;
using Group16.Controller;

namespace Group16.ElectronicDeviceSaleForm
{
    public partial class ElectronicDeviceSaleForm : Form
    {
        private Account _account;
        public ElectronicDeviceSaleForm()
        {
            InitializeComponent();
            DisableAuthority();
        }
        public Account EmployeeAcc
        {
            get
            {
                if (_account == null)
                    _account = new Account();
                return _account;
            }
            set { _account = value; }
        }

        /// <summary>
        /// 1:Check whether there is anny account used
        ///     if not do nothing
        ///     if yes Ask user to log out
        ///         if one decide to log out
        ///             Show LogInForm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuLogin_Click(object sender, EventArgs e)
        {
            DialogResult result = System.Windows.Forms.DialogResult.None;
            if (!String.IsNullOrEmpty(EmployeeAcc.UserName))
            {
                result = MessageBox.Show("You will sign out this account?", "log out", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
            if (result != System.Windows.Forms.DialogResult.No)
            {
                using (LoginForm log = new LoginForm())
                {
                    log.ShowDialog();
                    EmployeeAcc = log.Account;
                }
                if (String.IsNullOrEmpty(EmployeeAcc.UserName))
                    DisableAuthority();
                else
                    EnableAuthority();
            }
            if (String.IsNullOrEmpty(EmployeeAcc.UserName))
                this.Text = "None User";
            else
                this.Text = EmployeeAcc.UserName;
        }
        private void DisableAuthority()
        {
            this.searchAndSellItemToolStripMenuItem.Enabled = false;
            this.searchInvoiceToolStripMenuItem.Enabled = false;
            this.makeIncomeStatementToolStripMenuItem.Enabled = false;
            this.menuClose.Enabled = false;
            this.menuCloseAll.Enabled = false;
        }
        private void EnableAuthority()
        {
            this.searchAndSellItemToolStripMenuItem.Enabled = true;
            this.searchInvoiceToolStripMenuItem.Enabled = true;
            this.makeIncomeStatementToolStripMenuItem.Enabled = true;
            this.menuClose.Enabled = true;
            this.menuCloseAll.Enabled = true;
        }
        private void searchAndSellItemToolStripMenuItems_Click(object sender, EventArgs e)
        {
            SaleForm sdf = new SaleForm();
            sdf.MdiParent = this;
            sdf.Show();
        }

        private void searchInvoiceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SearchInvoice sif = new SearchInvoice();
            sif.MdiParent = this;
            sif.Show();
        }

        private void makeIncomeStatementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IncomeStatement inc = new IncomeStatement();
            inc.MdiParent = this;
            inc.Show();
        }

        private void menuWindowItem_Click(object sender, EventArgs e)
        {
            ToolStripItem item = sender as ToolStripItem;
            if (item != null)
            {
                string val = item.Tag as string;
                if (val != null)
                {
                    LayoutMdi((MdiLayout)Enum.Parse(typeof(MdiLayout), val));
                }
            }
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            if (ActiveMdiChild != null)
                ActiveMdiChild.Close();
        }

        private void menuCloseAll_Click(object sender, EventArgs e)
        {
            foreach (Form f in MdiChildren)
                f.Close();
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            using (AboutForm about = new AboutForm())
            {
                about.ShowDialog();
            }

        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            if (MdiChildren.Count() > 0)
            {

                DialogResult d = MessageBox.Show("Would you like to ex it this program?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (d == System.Windows.Forms.DialogResult.Yes)
                    Close();
                else
                    return;
            }
            Close();
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (DataBaseInteraction.Connection != null)
                DataBaseInteraction.Connection.Close();
            base.OnFormClosing(e);

        }
    }
    
}
